package com.zhang.teachingcommunity_spring.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.mapper.entity.User;
import com.zhang.teachingcommunity_spring.mapper.UserMapper;
import com.zhang.teachingcommunity_spring.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public String save(User user){
        if (user.getUserId() == null){
            int insert = userMapper.insert(user);
            return "注册成功！";
        }else{
            int i = userMapper.updateById(user);
            return "更新成功！";
        }
    }
    public List<User> login(User user) {
        List<User> login = userMapper.login(user);
       return login;
    }
    public User geById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }
    public void deleteById(Integer id) {
        int i = userMapper.deleteById(id);
    }
    public   List<User> searchUserName(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("username", username);
        userQueryWrapper.eq("user_type", 1);
        List<User> users = userMapper.selectList(userQueryWrapper);
        return users;
    }
    public IPage<User> findAll(User user,Page Ipage) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if(user.getSchoolId() != null && !(user.getSchoolId().equals(""))) {
            userQueryWrapper.eq("school_id", user.getSchoolId());
        }
        if(user.getUserType() != null && !(user.getUserType().equals(""))) {
            userQueryWrapper.eq("user_type", user.getUserType());
        }
        if(user.getStuNo() != null && !(user.getStuNo().equals(""))) {
            userQueryWrapper.eq("stu_no", user.getStuNo());
        }
        IPage<User> page = new Page<>(Ipage.getCurrent(),Ipage.getSize());
        IPage<User> userIPage = userMapper.selectPage(page, userQueryWrapper);
        return userIPage;
    }

}
